package json.org.noahx.javavsjson;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.ParseException;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: noah
 * Date: 3/9/13
 * Time: 1:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class JsonSmartSerializationTest implements SerializationTest {
    @Override
    public String getTestName() {
        return "Json Smart";
    }

    @Override
    public Map<String, Object> testBytes2Map(byte[] bytes) {
        Map<String, Object> map = null;
        try {
            map = (Map<String, Object>) JSONValue.parseStrict((new String(bytes, "UTF-8")));
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public byte[] testMap2Bytes(Map<String, Object> map) {
        String str = JSONObject.toJSONString(map);
        byte[] result = null;
        try {
            result = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}
